<script setup>
import { ref } from 'vue'
const userInfo = ref({
    oldpassword:'',
    newpassword:'',
    repassword:''
})
//校验密码函数
const checkRePassword=(rule,value,callback)=>{
    if(value===''){
        callback(new Error('请再次确认密码'))
    }else if(value!==userInfo.value.newpassword){
        callback(new Error('俩次输入的密码不一样'))
    }else{
        callback()
    }
}
const rules = {
    oldpassword: [
        { required: true, message: '请输入新密码', trigger: 'blur' },
        {
            pattern: /^\S{5,16}$/,
            message: '密码必须是5-16位的非空字符串',
            trigger: 'blur'
        }
    ],
    newpassword: [
        { required: true, message: '请输入新密码', trigger: 'blur' },
        {
            pattern: /^\S{5,16}$/,
            message: '密码必须是5-16位的非空字符串',
            trigger: 'blur'
        }
    ],
    repassword: [
    {validator:checkRePassword,trigger:'blur'}
    ]
}
import { userPasswordUpdateService } from '@/api/user'
import { ElMessage ,ElMessageBox} from 'element-plus'
import {useRouter} from 'vue-router'
const router=useRouter()

const resetPassword = async() => {
    console.log('userInfo.value:', userInfo.value); // 调试输出
    
        let result = await userPasswordUpdateService(userInfo.value);
        if (result.code === 0) {
            ElMessage.success(result.msg ? result.msg : '修改成功');
            router.push('/login');
            userInfoStore.setUserInfo(result.value);
            //跳转到登录界面
        } else {
            ElMessage.error(result.message || '操作失败');
        }
    
}
</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>基本资料</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="userInfo" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="旧密码" prop="oldpassword">
                        <el-input v-model="userInfo.oldpassword" ></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="newpassword">
                        <el-input v-model="userInfo.newpassword"></el-input>
                    </el-form-item>
                    <el-form-item label="确认新密码" prop="repassword">
                        <el-input v-model="userInfo.repassword"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button  type="primary" @click="resetPassword();handleCommand()">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>